/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo;

import java.net.URL;

import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.PlatformUI;
import org.toedo.logging.Log;
import org.toedo.utils.SystemUtils;

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication {
	
	public static final String ID = "toedo-core"; //$NON-NLS-1$

	private void setUserHome() {	
		Location instanceLoc = Platform.getInstanceLocation(); 
		try {
			instanceLoc.set(new URL("file", null, SystemUtils.applicationDirectory), false); //$NON-NLS-1$
		} catch (Exception e) {
			Log.getInstance(Application.class).error(e.getMessage());
		} 
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
	 */
	public Object start(IApplicationContext context) throws Exception {
		
		setUserHome();
		
		Log.getInstance(Application.class).info(Platform.getBundle(ID).getHeaders().get("Bundle-Name") + " version " + Platform.getBundle(ID).getHeaders().get("Bundle-Version") + " started."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		Log.getInstance(Application.class).info("Platform informations: " + Platform.getOSArch() + ", " + Platform.getOS() + ", " + Platform.getNL());		 //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		
		PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS, false);
		PlatformUI.getPreferenceStore().setValue(IWorkbenchPreferenceConstants.KEY_CONFIGURATION_ID, "toedo-core.KeyScheme"); //$NON-NLS-1$
		
		SystemUtils.createConfiguration();
		
		Display display = PlatformUI.createDisplay();
		try {
			int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
			if (returnCode == PlatformUI.RETURN_RESTART)
				return IApplication.EXIT_RESTART;
			else
				return IApplication.EXIT_OK;
		} finally {
			Log.getInstance(Application.class).info("Application stoped."); //$NON-NLS-1$
			display.dispose();
		}
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.equinox.app.IApplication#stop()
	 */
	public void stop() {
		final IWorkbench workbench = PlatformUI.getWorkbench();
		if (workbench == null)
			return;
		final Display display = workbench.getDisplay();
		display.syncExec(new Runnable() {
			public void run() {
				if (!display.isDisposed())
					workbench.close();
			}
		});
	}
}
